package org.shoy.web.controller;

import org.apache.log4j.Logger;
import org.shoy.core.model.TqQuestionEntity;
import org.shoy.core.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 主站Controller
 * Created by luoyong on 2016/3/29.
 */
@Controller
public class MainController {
    private Logger logger = Logger.getLogger(MainController.class);

    @Autowired
    QuestionRepository questionRepository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        logger.info("进入首页");
        return "index";
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public String test(@PathVariable(value = "id") String id, @RequestParam(value = "name", required = false) String name, Map<String, Object> map) {
        System.out.println(String.format("%s:%s", id, name));
        map.put("id", id);
        map.put("name", name);
        return "users";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ModelAndView user() {
        ModelAndView modelAndView = new ModelAndView("users");
        modelAndView.addObject("time", new Date());
        return modelAndView;
    }

    @RequestMapping(value = "/questions")
    public ModelAndView questions() {
        ModelAndView modelAndView = new ModelAndView("questions");
        List<TqQuestionEntity> list = questionRepository.findAll();
        modelAndView.addObject("list", list);
        return modelAndView;
    }
}
